<?php 
class JobModel extends Model {
	public function __construct(){
		$this->open(Model::MYSQL);
		$this->calledClass(get_class($this));
	}
	public function display(){
		return $this->getRecords($fields);
	}
	public function findArray($where=array()){
		return parent::findAsArray($where);
	}
	public function getJobPublish(){
		$this->open (Model::MYSQL);
		$query="select * from Jobs where publish=1";
		$result = parent::query($query);
		$rows=array();
		while($row = mysql_fetch_object($result)){
			$rows[]=$row;
		}
		return $rows;
	}
	
	public function updateProve($jid = 0, $approve="N"){
		
		//echo "UPDATE Jobs set approve = '$approve' WHERE Jobs.id = '$jid' " ;
		return $this->query("UPDATE Jobs set approved = '$approve' WHERE Jobs.id = '$jid' ");
	}
	public function updateExpireJob(){
		
		$query="UPDATE Jobs set expired = 'Y' WHERE expired='N' AND NOW() > deadline " ;
		$this->query($query);
	}
	public function sendEmailToEmployee($jid){
		$query="SELECT Jobs.title as jtitle, Jobs.location as  jlocation, Jobs.post_date as jpost, Employers.email as  eemail
				FROM Employers
				INNER JOIN Jobs ON Employers.id = Jobs.user_id
				where Jobs.id=".$jid;
		$result = parent::query($query);
		$row = mysql_fetch_object($result);
		return $row;
	
	}
	public function getDayExpired(){
		$this->open (Model::MYSQL);
		$query="SELECT  Posts.post_date From  Posts ";
		$result = parent::query($query);
		while($row = mysql_fetch_object($result)){
			$rows[]=$row;
		}
		return $rows;
	}
	public function getExpiredToEmployee(){
		$this->open (Model::MYSQL);
		$query="SELECT Employers.email as eemail,Jobs.post_date, Jobs.title, Jobs.deadline as expire_date
				FROM Employers
				INNER JOIN Jobs ON Employers.id = Jobs.user_id Where Jobs.expired='N' ";
		$result = parent::query($query);
		while($row = mysql_fetch_object($result)){
			$rows[]=$row;
		}
		return $rows;
	}
	public function getPublicSendMailToAdmin($jid){
		$this->open (Model::MYSQL);
		$query="SELECT Employers.email as eemail,Employers.username,Jobs.title,Jobs.posting_date,Jobs.telephone,Jobs.posted_by
				FROM Employers
				INNER JOIN Jobs ON Employers.id = Jobs.user_id Where Jobs.id=".$jid;
		$result = parent::query($query);
		$row = mysql_fetch_object($result);
		return $row;
	}
    public function SelectC(){
		
		$selectC=array();
		$result = parent::query("SELECT id,name,parent_id FROM Categories");
		while ($rowsget = mysql_fetch_object($result)){
			$selectC[]=$rowsget;
		}
		return $selectC;
	}
	public function getJob($id){
		$this->open (Model::MYSQL);
		$query="SELECT * FROM Jobs WHERE id='$id' ";
		$result = parent::query($query);
		$rows=array();
		while($row = mysql_fetch_object($result)){
			$rows[]=$row;
		}
		return $rows;
	}
	//search fields with where as object
	public function getAsObject($feild=array(),$where=array()){
		
		return parent::getAsObject($feild,$where);
	}
	public function getJobsAsArray(){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Jobs ;" ;
		$result = parent::query($query);
		$rows=array();
		while($row = mysql_fetch_object($result)){
			$rows[$row->id]=$row;
		}
		return $rows;
	}
	public function getAsObjectAdvanceSearch($fields=array(),$where=array()){
		
		return parent::getAsObject($fields,$where);
	}
	
	//search fields with where as arrayKey
	public function getAsArray($fields=array(),$where=array()){
		
		return parent::getAsArray($fields);
	}
	
	public function getObject($fields=array()){
		
		return parent::findAsObject($fields);
	}
	public function getList($page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria="",$wheres=array()){
			$page=$page<1?1:$page;
			$perpage=$perpage<1?20:$perpage;
			$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
			$query = " SELECT Jobs.*,Employers.company_name , Employers.img_id FROM Jobs, Employers WHERE Employers.id = Jobs.user_id AND publish ='Y' AND Jobs.expired = 'N' AND Jobs.deadline >= CURDATE() " ;
			
			if(is_array($wheres)&&!empty($wheres)){
				$tempWhereFields=array();
				foreach($wheres AS $field => $val  ){
					if(!empty($val)){
						$tempWhereFields[]= $field ."'".$this->stringEscape($val)."'"  ;
					}
				}
				if(!empty($tempWhereFields)){
					$query .= " AND ";
				}
				$query .= implode(" AND ",$tempWhereFields);
			}
			
			if(!empty($searchFields)&&(isset($search)&&$search!=="")){
				$tempSearchFields=array();
				foreach($searchFields AS $field ){
					$tempSearchFields[]= " `$field` LIKE '%".$this->stringEscape($search)."%' " ;
				}
				$query .= " AND ( " . implode(" OR ",$tempSearchFields) . " ) ";
			}
			if(!empty($sortFields)){
				$tempSortFields=array();
				foreach($sortFields AS $field ){
					$tempSortFields[]= " `$field` " ;
				}
				$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
			}
			$totalRecords = $this->numRows($this->query($query));
			$totalPages = ceil($totalRecords/$perpage);
			$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
			$result = parent::query($query);
			if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false){
				$query1="SELECT Galleries.* FROM Galleries WHERE id in ($row->img_id); " ;
				$result1=$this->query($query1);
				while($row1=mysql_fetch_object($result1))$row->img_id=$row1;
				$result[]=$row;
			}
		}
			return array("search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	public function getEmployerJobs($empid , $page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria=""){
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$query = " SELECT * FROM Jobs " ;
		if(!empty($searchFields)&&(isset($search)&&$search!=="")){
			$tempSearchFields=array();
			foreach($searchFields AS $field ){
				$tempSearchFields[]= " `$field` LIKE '%".$this->stringEscape($search)."%' " ;
			}
			$query .= " WHERE " . implode(" OR ",$tempSearchFields) . " AND emp_id=$empid " ;
		}else{
			$query .= " WHERE user_id=$empid ";
		}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = $this->numRows($this->query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = $this->query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
		
	}
	
	public function getCandidateJobs($empid , $page=1,$perpage=20,$search="",$searchFields=array(),$sortFields=array(),$sortCriteria=""){
	
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$query = " SELECT * FROM Jobs " ;
		if(!empty($searchFields)&&(isset($search)&&$search!=="")){
			$tempSearchFields=array();
			foreach($searchFields AS $field ){
				$tempSearchFields[]= " `$field` LIKE '%".$this->stringEscape($search)."%' " ;
			}
			$query .= " WHERE " . implode(" OR ",$tempSearchFields) . " AND emp_id=$empid " ;
		}else{
			$query .= " WHERE user_id=$empid ";
		}
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = $this->numRows($this->query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = $this->query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	
	}
	public function delete($where=array(),$deleteSet=false){
		
		return parent::delete($where,$deleteSet);
	}
	public function numRows($result){
		
		return is_resource($result)?mysql_num_rows($result):null;
	}
	public function queryDropdown($id){
		
		$query="select id,name,parent_id from Categories where parent_id=$id";
		$result = parent::query($query);
		$rows=array();
		while($row = mysql_fetch_object($result)){  
			$rows[]=$row;
		}
		return $rows;
	}
	
	public function getJobToEdit($job_id){
		$this->open(Model::MYSQL);
		//$query="SELECT Jobs.*, Employers.company_name as by_company FROM Jobs, Employers WHERE Jobs.id=$job_id AND Jobs.user_id = Employers.id ";
		//$query="SELECT Jobs.*, Employers.* FROM Jobs, Employers WHERE Jobs.id=$job_id AND Jobs.user_id = Employers.id ";
		$query="SELECT Jobs.*, Jobs.posted_by as by_company FROM Jobs, Employers WHERE Jobs.id=$job_id AND Jobs.user_id = Employers.id ";
		$result=parent::query($query);
		$records=array();
		while($row=mysql_fetch_object($result)){
			/* $query = "SELECT Galleries.* FROM Galleries WHERE Galleries.id in ($row->img_id) ;" ;
			$result1 = parent::query($query);
			while($row1=mysql_fetch_object($result1))$row->img_id=$row1; */
			$records=$row;
		}
		return $records;
	}
	
	public function save($fields=array()){
		
		return parent::save($fields);
	}
	public function edit($fields=array(),$where=array()){
		
		return parent::update($fields,$where);
	}

	public function findObject($where=array()){
		
		return $this->findAsObject($where);
	}
	public function version($id){
		$query = "SELECT version FROM Jobs WHERE id='" .$id. "'";
		$result = parent::query($query);
		$row = mysql_fetch_object($result);
		return $row->version + 1;
	}
	public static function stringEscape($string){
			return mysql_real_escape_string($string);
	}
	public function getListofUser($table="",$page=1,$perpage=20,$search="",$searchFields = array()  ){
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$query = " SELECT * FROM $table " ;
		if(!empty($searchFields)&&(isset($search)&&$search!=="")){
			$tempSearchFields=array();
			foreach($searchFields AS $field ){
				$tempSearchFields[]= " `$field` LIKE '%".$search."%' " ;
			}
			$query .= " WHERE " . implode(" OR ",$tempSearchFields);
		}
	
		$totalRecords = $this->numRows($this->query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = $this->query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	public function getAdvanceSearchJobAll($page=1,$perpage=20,$search = array(), $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT * FROM Jobs " ;
		
		$query.=" Where ";
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			$query .= implode(" $operation ",$tempSearchFields);
		}
	
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	public function getAdvanceSearchJob($page=1,$perpage=20,$search = array(),$wheres=array(), $sortFields = array() , $sortCriteria = "DESC", $operation = "AND" ){
		
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		
		$query = "SELECT Jobs.* , Categories.name as job_category_name FROM
				(SELECT Jobs.*,Categories.name as job_time_name FROM
				(SELECT Jobs.* , Categories.name as location_name from Jobs , Categories WHERE Categories.id=Jobs.location ) AS Jobs , Categories
				WHERE Categories.id=Jobs.job_type) as Jobs , Categories WHERE Categories.id=Jobs.job_category " ;
		
		$isWhere=false;
		
		if(is_array($wheres)&&!empty($wheres)){
			$tempWhereFields=array();
			foreach($wheres AS $fieldOperator => $val ){
				$tempWhereFields[]= $fieldOperator."'".$this->stringEscape($val)."' " ;
			}
			$query .= " AND " .  implode(" AND ",$tempWhereFields);
			$isWhere=true;
		}
		
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			$query .= !empty($tempSearchFields)?" AND "  . " ( " . implode(" AND ",$tempSearchFields) . " ) ":"";
		}
		
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= !empty($tempSortFields)?" ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " :"";
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false){
				/*
				$query1="SELECT Galleries.* FROM Galleries WHERE id in ($row->img_id); " ;
				$result1=$this->query($query1);
				while($row1=mysql_fetch_object($result1))$row->img_id=$row1;
				*/
				$result[]=$row;
			}
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	public function getSearchPremium($page=1,$perpage=20,$search = array(),$wheres=array(), $sortFields = array() , $sortCriteria = "DESC", $operation = "AND" ){
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		
		$query = "SELECT Jobs.*, Employers.company_name, Employers.email, Employers.about_me , Employers.img_id FROM Employers, Jobs WHERE post_mode=2 AND user_id=Employers.id AND publish ='Y' AND approved = 'Y' AND Jobs.expired = 'N' AND Jobs.deadline >= CURDATE()  " ;
		
		$isWhere=false;
		
		if(is_array($wheres)&&!empty($wheres)){
			$tempWhereFields=array();
			foreach($wheres AS $fieldOperator => $val ){
				$tempWhereFields[]= $fieldOperator."'".$this->stringEscape($val)."' " ;
			}
			$query .= " AND " .  implode(" AND ",$tempWhereFields);
			$isWhere=true;
		}
		
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			$query .= !empty($tempSearchFields)?" AND "  . " ( " . implode(" OR ",$tempSearchFields) . " ) ":"";
		}
		
		$query .=" ORDER BY id DESC";
		/*
		if(!empty($sortFields)){
					$tempSortFields=array();
					foreach($sortFields AS $field ){
						$tempSortFields[]= " `$field` " ;
					}
					$query .= !empty($tempSortFields)?" ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " :"";
				}*/
		
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false){
				$query1="SELECT Galleries.* FROM Galleries WHERE id in ($row->img_id); " ;
				$result1=$this->query($query1);
				while($row1=mysql_fetch_object($result1))$row->img_id=$row1;
				$result[]=$row;
			}
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	public function getSearchSilver($page=1,$perpage=20,$search = array(),$wheres=array(), $sortFields = array() , $sortCriteria = "DESC", $operation = "AND" ){
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT Jobs.*, Employers.company_name, Employers.email, Employers.about_me , Employers.img_id FROM Employers, Jobs WHERE post_mode=1 AND user_id=Employers.id AND publish ='Y' AND approved = 'Y' AND Jobs.expired = 'N' AND Jobs.deadline >= CURDATE()  " ;
		$isWhere=false;
		if(is_array($wheres)&&!empty($wheres)){
			$tempWhereFields=array();
			foreach($wheres AS $fieldOperator => $val ){
				$tempWhereFields[]= $fieldOperator."'".$this->stringEscape($val)."' " ;
			}
			$query .= " AND " .  implode(" AND ",$tempWhereFields);
			$isWhere=true;
		} 
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			
			$query .= !empty($tempSearchFields)?" AND "  . " ( " . implode(" OR ",$tempSearchFields) . " ) ":"";
		}
		$query .=" ORDER BY id DESC";
		/*
		if(!empty($sortFields)){
					$tempSortFields=array();
					foreach($sortFields AS $field ){
						$tempSortFields[]= " `$field` " ;
					}
					$query .= !empty($tempSortFields)?" ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " :"";
					
				}*/
		
		//echo $query ;
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false){
				$query1="SELECT Galleries.* FROM Galleries WHERE id in ($row->img_id); " ;
				$result1=$this->query($query1);
				while($row1=mysql_fetch_object($result1))$row->img_id=$row1;
				$result[]=$row;
			}
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	public function getGoldJobs($page=1,$perpage=20,$search = array(),$wheres=array(), $sortFields = array() , $sortCriteria = "DESC", $operation = "AND" ){
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT Jobs.*, Employers.company_name, Employers.email, Employers.about_me , Employers.img_id FROM Employers, Jobs WHERE post_mode=2 AND user_id=Employers.id AND publish ='Y' AND approved = 'Y' AND Jobs.expired = 'N' AND Jobs.deadline >= CURDATE() " ;
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			$query .= !empty($tempSearchFields)?" AND "  . " ( " . implode(" AND ",$tempSearchFields) . " ) ":"";
		}
		$isWhere=false;
		if(is_array($wheres)&&!empty($wheres)){
			$tempWhereFields=array();
			foreach($wheres AS $fieldOperator => $val ){
				$tempWhereFields[]= $fieldOperator."'".$this->stringEscape($val)."' " ;
			} 
			$query .= " AND " .  implode(" AND ",$tempWhereFields);
			$isWhere=true;
		}
		
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= !empty($tempSortFields)?" ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " :"";
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false){
				if($row->img_id){
					$query1="SELECT Galleries.* FROM Galleries WHERE id in ($row->img_id); " ;
					$result1=$this->query($query1);
					while($row1=mysql_fetch_object($result1))$row->img_id=$row1;
				}
				$result[]=$row;
			}
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	public function get_active_platinum_jobs($page=1,$perpage=20,$search = array(),$wheres=array(), $sortFields = array() , $sortCriteria = "DESC", $operation = "AND" ){
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		
		$query = "SELECT Jobs.*, Employers.company_name, Employers.email, Employers.about_me , Employers.img_id FROM Employers, Jobs WHERE post_mode=1 AND user_id=Employers.id AND publish ='Y' AND approved = 'Y' AND Jobs.expired = 'N' AND Jobs.deadline >= CURDATE()  " ;
		
		$isWhere=false;
		if(is_array($wheres)&&!empty($wheres)){
			$tempWhereFields=array();
			foreach($wheres AS $fieldOperator => $val ){
				$tempWhereFields[]= $fieldOperator."'".$this->stringEscape($val)."' " ;
			}
			$query .= " AND " .  implode(" AND ",$tempWhereFields);
			$isWhere=true;
		}
		
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			$query .= !empty($tempSearchFields)?" AND "  . " ( " . implode(" AND ",$tempSearchFields) . " ) ":"";
		}
		
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= !empty($tempSortFields)?" ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " :"";
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false){
				$query1="SELECT Galleries.* FROM Galleries WHERE id in ($row->img_id); " ;
				$result1=$this->query($query1);
				while($row1=mysql_fetch_object($result1))$row->img_id=$row1;
				$result[]=$row;
			}
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
/*
	public function getStandardJobs($num=0){
		$query = "SELECT Jobs.* , Categories.name as job_category_name FROM
		(SELECT Jobs.*,Categories.name as job_time_name FROM
		(SELECT Jobs.* , Categories.name as location_name from Jobs , Categories WHERE Categories.id=Jobs.location ) AS Jobs , Categories
		WHERE Categories.id=Jobs.job_type) as Jobs , Categories
		WHERE Categories.id=Jobs.job_category AND Jobs.job_type='1' ORDER BY created DESC ".($num==0?"":" LIMIT 0,".$num." ")." ;" ;
		$result = $this->query($query);
		$rows=array();
		while($row=mysql_fetch_object($result)){
			$query="SELECT Galleries.* FROM Galleries WHERE id in ($row->img_id); " ;
			$result1=$this->query($query);
			while($row1=mysql_fetch_object($result1))$row->img_id=$row1;
			$rows[]=$row;
		}
		return $rows;
	}*/

	
	public function getJobCategory($num=0){
		$query = "SELECT Jobs.* , Categories.name as job_category_name FROM
		(SELECT Jobs.*,Categories.name as job_time_name FROM
		(SELECT Jobs.* , Categories.name as location_name from Jobs , Categories WHERE Categories.id=Jobs.location ) AS Jobs , Categories
		WHERE Categories.id=Jobs.job_type) as Jobs , Categories
		WHERE Categories.id=Jobs.job_category  ORDER BY created DESC ".($num==0?"":" LIMIT 0,".$num." ")." ;" ;
		$result = $this->query($query);
		$rows=array();
		while($row=mysql_fetch_object($result))$rows[]=$row;
		return $rows;
	}
	public function getJobByCat($cat_id){
		$this->open (Model::MYSQL);
		$query="SELECT * FROM Jobs WHERE job_categories='$cat_id' ";
		$result = parent::query($query);
		$rows=array();
		while($row = mysql_fetch_object($result)){
			$rows[]=$row;
		}
		return $rows;
	}
	public function getPage($user_id,$page=1,$perpage=20, $sortFields = array() , $sortCriteria = "ASC", $operation = "AND" ){
		
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT Jobs.*, Employers.company_name as by_company from Jobs , Employers WHERE user_id = $user_id and publish ='Y' and Jobs.user_id=Employers.id" ;
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= " ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " ;
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	
	//Author Mr. Phireak 
	/* This function get all job post type as standard and premium */
	
	/* Developer: Dim sidet
	 * Date Modify: 19-March-2014
	 * Return : number of standard(free) and premium rows 
	 * */
	public function getNumberOfJobPost($empid,$job_type){
		$result = parent::query("SELECT COUNT(*) AS NUM FROM Jobs WHERE user_id=$empid AND post_mode='$job_type' " );
		$row = mysql_fetch_object($result);
		return $row->NUM;
	}
	public function getNumberOfJobPostSP($empid,$job_type){
		$result = parent::query("SELECT post_mode,COUNT(*) AS num_job_post FROM Jobs WHERE user_id=$empid AND post_mode='$job_type' " );
		$row = mysql_fetch_object($result);
		return $row;
	}
	public function getSilver(){
		$result = parent::query("SELECT post_mode FROM Jobs WHERE user_id=$empid AND post_mode=1" );
		$row = mysql_fetch_object($result);
		return $row->post_mode;
	}
	public function getPublisJob($jid){
		$query="SELECT publish_date FROM Jobs Where id =".$jid;
		$result = parent::query($query);
		$row = mysql_fetch_object($result);
		return $row->publish_date;
	}
	public function getApproveJob($jid){
		$query="SELECT approved FROM Jobs Where id ='".$jid."'";
		$result = parent::query($query);
		$row = mysql_fetch_object($result);
		return $row->approved;
	}
	public function getIDjob($jid){
		$query="SELECT id FROM Jobs Where id ='".$jid."'";
		$result = parent::query($query);
		$row = mysql_fetch_object($result);
		return $row->id;
	}
	public function getGold(){
		$result = parent::query("SELECT post_mode FROM Jobs WHERE user_id=$empid AND post_mode=2" );
		$row = mysql_fetch_object($result);
		return $row->post_mode;
	}
	/* @Develop by : Mr.Dim sidet
	*  @Create date: 9-April
	*  @Return : img_id of logo compnay
	* */
	public function getImageIDofEmployer($jid){
		$query = " SELECT Employers.img_id FROM Employers RIGHT JOIN Jobs ON Jobs.user_id = Employers.id Where Jobs.id=".$jid;
		$result = parent::query($query);
		$row=mysql_fetch_object($result);
		return $row->img_id;
	}
	/* @Develop by : Mr.Dim sidet
	 * @Create date: 9-April 
	 * @Return : overview job by id
	 * */
	public function getOverviewJobs( $where=array() ,$jid=""){
		@$img_id=$this->getImageIDofEmployer($jid);
		if($img_id!=Null &&  $img_id!=0){
			$query = " SELECT Jobs.*,Employers.company_name,Employers.img_id, Employers.about_me,Galleries.location as loc_g FROM Employers RIGHT JOIN Jobs ON Jobs.user_id = Employers.id   ";
		}else{
			$query = " SELECT Jobs.*,Employers.company_name, Employers.img_id , Employers.about_me FROM Employers RIGHT JOIN Jobs ON Jobs.user_id = Employers.id   ";
		}
		if($img_id!=Null &&  $img_id!=0 ){
			$query.=" INNER JOIN Galleries ON Employers.img_id = Galleries.id " ;
		}
		if(is_array($where)&&!empty($where)){
			$tempSearchFields=array();
			foreach($where as $key => $val ){
				if($val!=""&&$val!=NULL)$tempSearchFields[]= " $key'$val' " ;
			}
			$query .= !empty($tempSearchFields)?" WHERE " .  implode(" OR ",$tempSearchFields):"";
			//print_r($query);
		}
		$result = parent::query($query);
		$row=mysql_fetch_object($result);
		return $row;
	}
	public function getMultiRecord($cat_id){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Jobs  WHERE job_category =  '$cat_id'  AND approved =  'Y'  AND expired =  'N'  AND publish =  'Y' GROUP BY user_id  ; ";
		$result = parent::query($query);
		$candidate = array();
		while($rows = mysql_fetch_object($result)){
			$candidate[] = $rows;
		}
		return $candidate;
	}
	/* get job all job categories from jobs table */
	public function getJobCategories(){
		$query = "SELECT job_category FROM Jobs GROUP BY job_category " ;
		$result = $this->query( $query );
		$data = array();
		while($row=mysql_fetch_object($result))$data[]=$row->job_category;
		return $data;
	}
	public function getJobLocations(){
		$query = "SELECT location_category FROM Jobs GROUP BY location_category " ;
		$result = $this->query( $query );
		$data = array();
		while($row=mysql_fetch_object($result))$data[]=$row->location_category;
		return $data;
	}
	public function getJobTypes(){
		$query = "SELECT job_type FROM Jobs GROUP BY job_type " ;
		$result = $this->query( $query );
		$data = array();
		while($row=mysql_fetch_object($result))$data[]=$row->job_type;
		return $data;
	}
	//Mr.Roth
	public function GetSilverjob(){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Jobs WHERE post_mode=1 AND publish='Y' AND Jobs.deadline > NOW()" ;
		$result = mysql_query($query);
		return mysql_num_rows($result);
	}
	public function checkCategory($cat_name) {
		$this -> open(Model::MYSQL);
		$query = "select id from Categories where name='".mysql_real_escape_string($cat_name)."'";
		$result = parent::query($query);
		return mysql_num_rows($result) ? true : false;
	}
	public function getCategoryId($cat_name){
		$this -> open(Model::MYSQL);
		$query = "select id from Categories where name='".mysql_real_escape_string($cat_name)."'";
		$row = array();
		$result = parent::query($query);
		$row = mysql_fetch_object($result);
		return $row->id;
	
	}
	public function getSearchSet($id){
		$this -> open(Model::MYSQL);
		$query = "select search_set from Categories where id=".$id;
		$row = array();
		$result = parent::query($query);
		$row = mysql_fetch_object($result);
		return $row;
	
	}
        //this function display at the home page no pagination listed all gold jobs
	public function get_active_gold_jobs(){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Jobs  WHERE post_mode=2 AND publish='Y' AND approved='Y' AND deadline>=CURDATE() ORDER BY id DESC";
		$result = parent::query($query);
		$rows = array();
		while($row = mysql_fetch_object($result)){
			$rows[] = $row;
		}
		return $rows;
	}
	//get number of record job premium active
	public function get_active_premium_jobs(){
		$this->open(Model::MYSQL);
		$premium = parent::query("SELECT * FROM Jobs WHERE Jobs.post_mode=2 AND Jobs.publish='Y' AND Jobs.deadline>=CURDATE()");
		return mysql_num_rows($premium);
	}
	//get number of record job standard active
	public function get_active_standard_jobs(){
		$this->open(Model::MYSQL);
		$standard = parent::query("SELECT * FROM Jobs WHERE Jobs.post_mode=1 AND Jobs.publish='Y' AND Jobs.deadline>=CURDATE()");
		return mysql_num_rows($standard);
	}
	//get number of jobs posted by specific date
	public function get_job_publish_by_date($date){
		$this->open(Model::MYSQL);
		$jobs = parent::query("SELECT * FROM Jobs WHERE Jobs.publish_date='$date'");
		return mysql_num_rows($jobs);
	}
	//get number of jobs posted between two date
	public function get_rank_of_job_publish($start,$end){
		$this->open(Model::MYSQL);
		$jobs = parent::query("SELECT * FROM Jobs WHERE Jobs.publish_date>='$start' AND Jobs.publish_date<='$end'");
		return mysql_num_rows($jobs);
	}
	//update job hit within one hour left after posted
	public function update_job_hit(){
		$date = date("Y-m-d") ;
		$query= "SELECT * FROM Jobs WHERE publish_date = '$date' " ; 
		$result = parent::query($query);
		while($row = mysql_fetch_object($result)){
			$hour = (strtotime("now") - strtotime($row->post_date))%86400/3600;
			if( $hour >= 1 && $hour <=1.2 ){  // posted one hour or over one hour ago update hit on field job
				if( $row->hits <= 80 ){
					$this->edit(array("hits"=>$row->hits + 80),array("id="=>$row->id));
				}
			}
		}
	}
	public function get_standard_internal(){
		$this->open(Model::MYSQL);
		$query = "SELECT COUNT(*) as Total FROM Jobs WHERE user_id IN(1585,2571,309,2567,7015,6964) AND post_mode=1 AND post_date LIKE '".date('Y-m-d')."%'";
		$result = parent::query($query);
		$result =  mysql_fetch_assoc($result);
		return $result["Total"]; 
	}
	public function get_standard_external(){
		$this->open(Model::MYSQL);
		$query = "SELECT COUNT(*) as Total FROM Jobs WHERE post_mode=1 AND post_date LIKE '".date('Y-m-d')."%'";
		$result = parent::query($query);
		$result =  mysql_fetch_assoc($result);
		return $result["Total"]-$this->get_standard_internal(); 
	}
	//get job posted by internal or external as standard job
	public function get_premium_internal(){
		$this->open(Model::MYSQL);
		$query = "SELECT COUNT(*) as Total FROM Jobs WHERE user_id IN(1585,2571,309,2567,7015,6964) AND post_mode=2 AND post_date LIKE '".date('Y-m-d')."%'";
		$result = parent::query($query);
		$result =  mysql_fetch_assoc($result);
		return $result["Total"]; 
	}
	//get job posted by internal or external as premium job
	public function get_premium_external(){
		$this->open(Model::MYSQL);
		$query = "SELECT COUNT(*) as Total FROM Jobs WHERE post_mode=2 AND post_date LIKE '".date('Y-m-d')."%'";
		$result = parent::query($query);
		$result =  mysql_fetch_assoc($result);
		return $result["Total"]-$this->get_premium_internal(); 
	}
	
	//Display Job in a day by roth
	public function getJobdisplay($page=1,$perpage=20,$start_date="",$end_date=""){
		$this->open(Model::MYSQL);
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$query ="SELECT *  FROM Jobs";
		$query.=" Where DATE_FORMAT(posting_date,'%Y-%m-%d') = '".date("Y-m-d")."' AND title<>'' AND  posted_by<>'' AND job_category<>'' AND  job_type<>'' " ;
		$query.=" ORDER BY posting_date DESC ";
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false)$result[]=$row;
		}
		return array("query"=>$query,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	/**
	 * @author Mr.Noeur Phireak
	 * this function to get all jobs from our databases
	 * @return total job posted
	 * Date : 2014-June-19
	 */
	public function get_total_job(){
		$this->open(Model::MYSQL);
		$result = parent::query("SELECT COUNT(*) as total FROM Jobs");
		$record = mysql_fetch_object($result);
		return $record->total;
	}
	/**
	 * @author Mr. Noeur Phireak
	 * this function to get all job by job type and currently active
	 * @param post_mode string of post mode (gold , platinum,urgent or free)
	 * @return number of currently active urgent job
	 * Date 2014-June-19
	 */
	public function get_active_urgent_jobs(){
		$this->open(Model::MYSQL);
		$query = "SELECT * FROM Jobs WHERE post_mode=4 AND deadline >= CURDATE() AND publish='Y' AND approved='Y'" ;
		$rows = array() ;
		$result = parent::query($query);
		while($row = mysql_fetch_object($result))
			$rows[] = $row;
		return $rows;
	}
	public function get_logo_employer(){
		$this->open(Model::MYSQL); 
		$rows = array() ;
		$result = parent::query("select Employers.id as emp_id,Employers.firstname,Employers.lastname,Galleries.id as gid,Galleries.`name`,Galleries.location From Employers,Galleries where Employers.img_id=Galleries.id");
		while($row = mysql_fetch_object($result))
			$rows[]=$row;
		return $rows;
	
	}
	public function get_active_free_jobs($page=1,$perpage=20,$search = array(),$wheres=array(), $sortFields = array() , $sortCriteria = "DESC", $operation = "AND" ){
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		
		$query = "SELECT Jobs.*, Employers.company_name, Employers.email, Employers.about_me , Employers.img_id FROM Employers, Jobs WHERE post_mode=0 AND user_id=Employers.id AND publish ='Y' AND approved = 'Y' AND Jobs.expired = 'N' AND Jobs.deadline >= CURDATE()" ;
		
		$isWhere=false;
		
		if(is_array($wheres)&&!empty($wheres)){
			$tempWhereFields=array();
			foreach($wheres AS $fieldOperator => $val ){
				$tempWhereFields[]= $fieldOperator."'".$this->stringEscape($val)."' " ;
			}
			$query .= " AND " .  implode(" AND ",$tempWhereFields);
			$isWhere=true;
		}
		
		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			$query .= !empty($tempSearchFields)?" AND "  . " ( " . implode(" AND ",$tempSearchFields) . " ) ":"";
		}
		
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= !empty($tempSortFields)?" ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " :"";
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false){
				$query1="SELECT Galleries.* FROM Galleries WHERE id in ($row->img_id); " ;
				$result1=$this->query($query1);
				while($row1=mysql_fetch_object($result1))$row->img_id=$row1;
				$result[]=$row;
			}
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
	/**
	 * @author Mr. Noeur Phireak
	 * @return get location and categor of job with number of job posted
	 * Date : 2014-June-19
	 */
	public function get_job_in_locations(){
		$result = parent::query( "SELECT Categories.id,Categories.name,COUNT( Jobs.location_category ) AS total FROM Categories,Jobs WHERE Categories.name=Jobs.location_category AND deadline >=CURDATE() AND publish='Y' AND approved='Y' GROUP BY location_category" );
		$rows = array();
		while ($row = mysql_fetch_object($result))
			$rows[]=$row;
		return isset($rows)?$rows:null;
	}
	public function get_job_in_categories(){
		$result = parent::query( "SELECT Categories.id,Categories.name, COUNT( Jobs.job_category ) AS total FROM Categories, Jobs WHERE Categories.name=Jobs.job_category AND deadline >=CURDATE() AND publish='Y' AND approved='Y' GROUP BY job_category" );
		$rows = array();
		while ($row = mysql_fetch_object($result))
			$rows[]=$row;
		return isset($rows)?$rows:null;
	}
	public function get_list_job_by_category($page=1,$perpage=20,$search = array(),$wheres=array(), $sortFields = array() , $sortCriteria = "DESC", $operation = "AND" ){
		$page=$page<1?1:$page;
		$perpage=$perpage<1?20:$perpage;
		$sortCriteria=$sortCriteria===""?"ASC":$sortCriteria;
		$operation=$operation===""?"AND":$operation;
		$query = "SELECT Jobs.*, Employers.company_name, Employers.email, Employers.about_me , Employers.img_id FROM Employers,Jobs,Categories WHERE Jobs.name='' AND user_id=Employers.id AND publish ='Y' AND approved = 'Y' AND Jobs.expired = 'N' AND Jobs.deadline >= CURDATE()";
		$isWhere=false;
		
		if(is_array($wheres)&&!empty($wheres)){
			$tempWhereFields=array();
			foreach($wheres AS $fieldOperator => $val ){
				$tempWhereFields[]= $fieldOperator."'".$this->stringEscape($val)."' " ;
			}
			$query .= " AND " .  implode(" AND ",$tempWhereFields);
			$isWhere=true;
		}

		if(isset($search)&&$search!==""){
			$tempSearchFields=array();
			foreach($search as $key=>$val){
				$tempSearchFields[]= " `$key` LIKE '%". $val ."%' " ;
			}
			$query .= !empty($tempSearchFields)?" AND "  . " ( " . implode(" AND ",$tempSearchFields) . " ) ":"";
		}
	
		if(!empty($sortFields)){
			$tempSortFields=array();
			foreach($sortFields AS $field ){
				$tempSortFields[]= " `$field` " ;
			}
			$query .= !empty($tempSortFields)?" ORDER BY " . implode(" , ",$tempSortFields) . " " . $sortCriteria . " " :"";
		}
		$totalRecords = mysql_num_rows(parent::query($query));
		$totalPages = ceil($totalRecords/$perpage);
		$query .= " LIMIT ".(($page-1)*$perpage)." , ".$perpage." ; " ;
		$result = parent::query($query);
		if($result!==null){
			$rows = $result ;
			$result = array();
			while(($row=mysql_fetch_object($rows))!==false){
				$query1="SELECT Galleries.* FROM Galleries WHERE id in ($row->img_id); " ;
				$result1=$this->query($query1);
				while($row1=mysql_fetch_object($result1))$row->img_id=$row1;
				$result[]=$row;
			}
		}
		return array("query"=>$query,"search"=>$search,"page"=>$page,"perpage"=>$perpage,"totalpages"=>$totalPages,"totalrecords"=>$totalRecords,"records"=>($result===null?array():$result)) ;
	}
}
?>
